/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
package example.springboot.service;

import example.springboot.dto.PageResult;
import example.springboot.dto.StudyRecordDTO;
import example.springboot.entity.StudyRecord;
import example.springboot.mapper.StudyRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudyRecordService {

    @Autowired
    private StudyRecordMapper studyRecordMapper;

    /**
     * 分页查询学习记录
     */
    public PageResult<StudyRecordDTO> getStudyRecordList(Integer userId, String startDate, String endDate, int page, int size) {
        int offset = (page - 1) * size;
        List<StudyRecordDTO> records = studyRecordMapper.findByPage(userId, startDate, endDate, offset, size);
        int total = studyRecordMapper.countRecords(userId, startDate, endDate);

        return new PageResult<>(records, (long) total);
    }

    /**
     * 根据ID查询学习记录
     */
    public StudyRecord findById(Integer id) {
        return studyRecordMapper.findById(id);
    }

    /**
     * 创建学习记录
     */
    public StudyRecord createStudyRecord(StudyRecord record) {
        studyRecordMapper.insert(record);
        return record;
    }

    /**
     * 删除学习记录
     */
    public boolean deleteStudyRecord(Integer id) {
        return studyRecordMapper.deleteById(id) > 0;
    }
}

